package com.weigj.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.weigj.model.CarInfo;
import com.weigj.model.CarType;
import com.weigj.model.Dept;
import com.weigj.model.ReserveHour;

public class CarTypeController extends Controller {
	
	//查询部门下的车型
	public void queryCarType() throws Exception{
		String date=getPara("reserveHour.reserveDate");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		Long deptId=getParaToLong("reserveHour.carLocalId");
		String deptName=getPara("reserveHour.TerminalName");
		Dept dept=Dept.deptDao.queryDeptByDeptName(deptName);
		List<CarType> carTypes=CarType.carTypeDao.queryCarTypeByDeptId(deptId,dept.getBigDecimal("FENCOM").longValue());
		String limit=sdf.parse(date).getDay()+"";
		Record record=CarType.carTypeDao.queryLimit(limit, dept.getBigDecimal("DEPTINCID").longValue());
		if(null!=carTypes&&carTypes.size()>0){
			for (int i = 0; i < carTypes.size(); i++) {
				CarType carType=carTypes.get(i);
				if(null!=record){
					carType.put("carCount", CarInfo.dao.queryCarByCarType(deptId,carType.getBigDecimal("TCM_ID").longValue(),record.getStr("SECNAME")));
				}else{
					carType.put("carCount", CarInfo.dao.queryCarByCarType(deptId,carType.getBigDecimal("TCM_ID").longValue(),null));
				}
			}
		}
		setAttr("dept", dept);
		setAttr("date", date);
		System.out.println(carTypes.size());
		setAttr("carTypes", carTypes);
		render("/yd_cartype.jsp");
	}
	//获取其他费用
	public void querOtherPrice(){
		ReserveHour reserveHour=getModel(ReserveHour.class);
		String memberlevel=getPara("#memberlevel");
		if(memberlevel==null||memberlevel.equals("")){
			memberlevel="普通会员";
		}
		Record record=CarType.carTypeDao.queryMember(memberlevel);
		Record record1=CarType.carTypeDao.queryOtherPrice(reserveHour.getBigDecimal("carTypeId").longValue(), reserveHour.getBigDecimal("usefencomid").longValue(), record.getBigDecimal("clasid").longValue());
		Record record2=CarType.carTypeDao.queryWzMoney(reserveHour.getBigDecimal("usefencomid").longValue());
		//定价中心不完善
		if(record1!=null){
			reserveHour.put("hourLimitKM", record1.get("ad_daylimitmeter"));
			reserveHour.put("overDriveFeeUnit", record1.get("ad_overdrivefee"));
		}
		if(null!=record2){
			reserveHour.put("carDeposit", record2.get("brp_factor"));
		}
		setAttr("reserveHour", reserveHour);
		setAttr("date",getPara("date"));
		render("/yd_tjdd.jsp");
	}
	
	//添加预定合同
	public void addReserveHour() throws Exception{
		ReserveHour reserveHour=getModel(ReserveHour.class);
		reserveHour.set("id", "app_reservehour_seq.nextval");
		reserveHour.set("reserveDate", getPara("date")+":00");
		reserveHour.set("reserveState", 1);
		//生成预订单号 格式YD+年月日时分秒毫秒
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String reserveNo="YD"+sdf1.format(new Date());
		reserveHour.set("reserveNo", reserveNo);
		String limit=new Date().getDay()+"";
		//限行规则
		Record record=CarType.carTypeDao.queryLimit(limit, reserveHour.getBigDecimal("usecomid").longValue());
		CarInfo car=new CarInfo();
		if(record!=null){
			car=CarInfo.dao.queryCarBytype(reserveHour.getBigDecimal("carLocalId").longValue(), reserveHour.getBigDecimal("carTypeId").longValue(),record.getStr("SECNAME"));
		}else{
			car=CarInfo.dao.queryCarBytype(reserveHour.getBigDecimal("carLocalId").longValue(), reserveHour.getBigDecimal("carTypeId").longValue(),null);
		}
		if(null!=car){
			reserveHour.set("carNum", car.get("infolicense"));
			reserveHour.set("infoframcode", car.get("infoframcode"));
			//更改车辆状态为预约
			car.set("carState", "预约");
			car.update();
		}else{
			render("/error.jsp");
			return;
		}
		Boolean result=reserveHour.save();
		if(result){
			setAttr("reserveHour", reserveHour);
			System.out.println(reserveHour+"==========");
			render("/yd_ddcg.jsp");
		}else{
			//render("/walkMap.jsp");
		}
	}
	//获取车辆的其他费用(json)
	public void queryOtherPriceJson(){
		Long typeId=getParaToLong("typeId");
		Long fenId=getParaToLong("fenId");
		String memberlevel=getPara("#memberlevel");
		if(memberlevel==null||memberlevel.equals("")){
			memberlevel="普通会员";
		}
		Record record=CarType.carTypeDao.queryMember(memberlevel);
		Record record1=CarType.carTypeDao.queryOtherPrice(typeId, fenId, record.getBigDecimal("clasid").longValue());
		Record record2=CarType.carTypeDao.queryWzMoney(fenId);
		ReserveHour rh=new ReserveHour();
		//定价中心不完善
		if(record1!=null){
			rh.put("hourLimitKM", record1.get("ad_daylimitmeter"));
			rh.put("overDriveFeeUnit", record1.get("ad_overdrivefee"));
		}
		if(null!=record2){
			rh.put("carDeposit", record2.get("brp_factor"));
			
		}
		renderJson("rh",rh);
	}
	
	public void queryAllCity(){
		List<Record> list=CarType.carTypeDao.queryAllCity();
		setAttr("citylist", list);
		setAttr("cityName", getPara("cityName"));
		render("/yd_city.jsp");
	}
	
}
